package com.happy.xiao.consumer.config;


import com.happy.xiao.common.rabbitmq.BaseMessageContainer;
import com.happy.xiao.consumer.rabbitmq.DLXMessageListener;
import com.happy.xiao.consumer.rabbitmq.SimpleListener;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConsumer extends BaseMessageContainer {

    @Autowired
    SimpleListener simpleListener;

    @Autowired
    DLXMessageListener dlxMessageListener;

    /**
     * 单一消费者
     *
     * @return
     */
    @Bean(name = "singleListenerContainer")
    public SimpleRabbitListenerContainerFactory listenerContainer() {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        factory.setConcurrentConsumers(1);
        factory.setMaxConcurrentConsumers(1);
        factory.setPrefetchCount(1);
        factory.setTxSize(1);
        return factory;
    }

    /**
     * 多个消费者
     *
     * @return
     */
    @Bean(name = "multiListenerContainer")
    public SimpleRabbitListenerContainerFactory multiListenerContainer() {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factoryConfigurer.configure(factory, connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());

        // 消息确认模式， MANUAL手动确认
        factory.setAcknowledgeMode(AcknowledgeMode.NONE);
        factory.setConcurrentConsumers(env.getProperty("spring.rabbitmq.listener.concurrency", int.class));
        factory.setMaxConcurrentConsumers(env.getProperty("spring.rabbitmq.listener.max-concurrency", int.class));
        factory.setPrefetchCount(env.getProperty("spring.rabbitmq.listener.prefetch", int.class));
        return factory;
    }

    /**
     * 这个是在rabbitMqConfig中配置的 【指定队列】
     *
     * @param simpleQueue
     * @return
     */
    @Bean(name = "simpleContainer")
    public SimpleMessageListenerContainer simpleContainer(@Qualifier("simpleQueue") Queue simpleQueue) {
        return createContainer(simpleListener, simpleQueue);
    }

    // TODO ------------------------------------------------------------------------------------------------------------

    /**
     * 公共的死信队列转发队列监听
     *
     * @param deadRealQueue
     * @return
     */
    @Bean(name = "deadRealContainer")
    public SimpleMessageListenerContainer deadRealContainer(@Qualifier("deadRealQueue") Queue deadRealQueue) {
        return createContainer(dlxMessageListener, deadRealQueue);
    }

    // TODO ------------------------------------------------------------------------------------------------------------



}
